//
//  UITabBarController+Anim.m
//  Orchard
//
//  Created by ios001 on 16/8/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "UITabBarController+Anim.h"

@implementation UITabBarController (Anim)

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    // 实现动画方法二
    // 取出当前点击按钮对应的索引
    NSInteger tabBarIndex = [tabBar.items indexOfObject:item];
    NSMutableArray * array = [NSMutableArray array];
    // 遍历tabBar的子控件,取出类型是UITabBarButton的控件
    for (UIView *btnView in tabBar.subviews) {
        if ([btnView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            // 将遍历到的控件保存到数组
            [array addObject:btnView];
        }
    }
    // 通过索引取出数组中对应的控件
    UIView * view = [array objectAtIndex: tabBarIndex];
    // 遍历btn的子控件,取出类型是UITabBarSwappableImageView的控件 然后动画
    for (UIView *imgView in view.subviews) {
        if ([imgView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]){
            // 1.创建关键帧动画
            [imgView.layer addAnimation:[self transformAnim:imgView] forKey:nil];
        }
    }
}

// 动画缩放效果实现一
- (id)transformAnim:(UIView *)imgView{
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    // 3.添加到layer上
    [imgView.layer addAnimation:anim forKey:nil];
    anim.duration = 0.5;
    // 2.设置属性
    anim.values = @[@1.5,@0.8,@1.3,@0.8,@1.2];
    anim.repeatCount = 1;
    return anim;
}


- (void)demo{
    // 实现动画方法一
    /*
     //    NSLog(@"%@",tabBar.subviews);
     NSInteger index = -1;
     //    NSInteger tabBarIndex = self.selectedIndex;
     NSInteger tabBarIndex = [tabBar.items indexOfObject:item];
     NSLog(@"%ld",tabBarIndex);
     for (UIView *view in tabBar.subviews) {
     
     if(![view isKindOfClass:NSClassFromString(@"UITabBarButton")]){
     continue;
     }
     index++;
     if (index == tabBarIndex) {
     for (UIView *imgView in view.subviews) {
     if ([imgView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
     [UIView animateWithDuration:0.25 animations:^{
     imgView.transform = CGAffineTransformMakeScale(1.5, 1.5);
     } completion:^(BOOL finished) {
     imgView.transform = CGAffineTransformIdentity;
     }];
     }
     }
     }
     }
     */
}

// 动画缩放效果实现二
- (void)demo2{
    /*
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];
    // 3.添加到layer上
    [imgView.layer addAnimation:anim forKey:nil];
    anim.duration = 0.5;
    // 2.设置属性
    
    anim.values = @[
                    [NSValue valueWithCGSize:CGSizeMake(40, 40)],
                    [NSValue valueWithCGSize:CGSizeMake(20, 20)],
                    [NSValue valueWithCGSize:CGSizeMake(30, 30)],
                    [NSValue valueWithCGSize:CGSizeMake(20, 20)],
                    [NSValue valueWithCGSize:CGSizeMake(25, 25)],
                    ];
    anim.repeatCount = 1;
    return anim;
     */
}

@end
